package org.java;

public class DoByte {
	byte[] bytes;
	final int totalnum = Integer.MAX_VALUE;	

	public DoByte() {
		int x = totalnum & 0x7;
		if (x > 0) {			
			bytes = new byte[totalnum << 3 + 1];
		} else {
			bytes = new byte[totalnum << 3];
		}

	}

	
	//i除8商为要更新的byte数组 下标，更新的值为i除8的余
	public void add(int i) {
		// x=i除8的余数
		int x = i & 0x7;
		if (x > 0) {
			bytes[(i >> 3) + 1] |= 1 << x;
		} else {
			bytes[(i >> 3)] |= 1;
		}
	}

//	i除8商为要更新的byte数组 下标，更新的值为i除8的余
	public boolean get(final int i) {
		int x = i & 0x7;
		if (x > 0) {
			return (x == bytes[(i >> 3) + 1]);
		} else {
			return (x == bytes[(i >> 3)]);
		}
	}

	public static byte[] intToBytes(int num) {

		byte[] b = new byte[4];
		for (int i = 0; i < 4; i++) {
			b[i] = (byte) (num >>> (24 - i * 8));
		}
		return b;
	}
	
	 public byte[] intToByte(int i) {

	        byte[] abyte0 = new byte[4];

	        abyte0[0] = (byte) (0xff & i);

	        abyte0[1] = (byte) ((0xff00 & i) >> 8);

	        abyte0[2] = (byte) ((0xff0000 & i) >> 16);

	        abyte0[3] = (byte) ((0xff000000 & i) >> 24);

	        return abyte0;

	    }
	 
	   public  static int bytesToInt(byte[] bytes) {

	        int addr = bytes[0] & 0xFF;

	        addr |= ((bytes[1] << 8) & 0xFF00);

	        addr |= ((bytes[2] << 16) & 0xFF0000);

	        addr |= ((bytes[3] << 24) & 0xFF000000);

	        return addr;

	    }

	public static void main(String[] args) throws InterruptedException {

		DoByte b = new DoByte();

		b.add(100);

		System.out.println(b.get(100));
		System.out.println(b.get(101));

		System.out.println((int) Math.ceil(65 / 8.0));
		System.out.println(65 & (~0x7));
		System.out.println(64 >> 3);
		System.out.println(1 << 66 % 8);
		System.out.println(1 << 66 & 0x7);

		// byte[][] buffer = null;

		// intToBytes(16);

		// for (int i = 0; i < 100; i++) {
		// Thread.sleep(10000);
		// buffer = new byte[1 * 1000 * 10000][4];
		// }
		// System.out.println(buffer[100]);
	}
}
